package com.scaffold.authority.entity;

import static javax.persistence.GenerationType.IDENTITY;

import java.util.LinkedHashSet;
import java.util.List;
import java.util.Set;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Transient;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

import wangjg.security.core.AuthoritySet;

/**
 * 用户
 * @author wangjg
 */
@Entity
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class User extends BasicBizEntity implements AuthoritySet{
	private static final long serialVersionUID = 1L;

	@Id
	@GeneratedValue(strategy = IDENTITY)
	private Long id;
	
	private String username;//用户名

	private String password;//密码
	
	// 是否启用 
	@Column(length=1)
	private String enabled;
	
	//角色 @ManyToMany User.id=UserRoleRelation.userId~UserRoleRelation.roleId=Role.id#name
	@Transient
	private List<Role> roles;
	
	//权限 @ManyToMany User.id=UserAuthorityRelation.userId~UserAuthorityRelation.authorityId=Authority.id#name
	@Transient
	private List<Authority> auths;
	
	@Transient
	private Set authorities ;
	
	public User() {
	}
	
	public Set getAuthorities() {
		if(authorities==null){
			authorities = new LinkedHashSet<Role>();
			if(this.roles!=null){
				authorities.addAll(this.roles);	
			}
			if(this.auths!=null){
				authorities.addAll(this.auths);
			}
		}
		return authorities;
	}
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getUsername() {
		return username;
	}
	public void setUsername(String username) {
		this.username = username;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public List<Authority> getAuths() {
		return auths;
	}

	public void setAuths(List<Authority> auths) {
		this.auths = auths;
	}

	public String getEnabled() {
		return enabled;
	}

	public void setEnabled(String enabled) {
		this.enabled = enabled;
	}

}
